﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Arch.Aspects
{
    public enum Access
    {
        Any = 0,
        Authentified = 1,
        None = 2,
    }

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class SecurityAccess : Attribute
    {
        private Access _readAccess;
        public Access ReadAccess
        {
            get { return _readAccess == Access.Any && !string.IsNullOrEmpty(ReadRoles) ? Access.Authentified : _readAccess; }
            set { _readAccess = value; }
        }

        private Access _deleteAccess;
        public Access DeleteAccess
        {
            get { return _deleteAccess == Access.Any && !string.IsNullOrEmpty(DeleteRoles) ? Access.Authentified : _deleteAccess; }
            set { _deleteAccess = value; }
        }

        private Access _updateAccess;
        public Access UpdateAccess
        {   
            get { return _updateAccess == Access.Any && !string.IsNullOrEmpty(UpdateRoles) ? Access.Authentified : _updateAccess; }
            set { _updateAccess = value; }
        }

        private Access _createAccess;
        public Access CreateAccess
        {
            get { return _createAccess == Access.Any && !string.IsNullOrEmpty(CreateRoles) ? Access.Authentified : _createAccess; }
            set { _createAccess = value; }
        }

        public string ReadRoles { get; set; }
        public string DeleteRoles { get; set; }
        public string CreateRoles { get; set; }
        public string UpdateRoles { get; set; }

        public Access WriteAccess
        {
            get { return UpdateAccess; }
            set
            {
                if (CreateAccess != Access.Any || UpdateAccess != Access.Any || DeleteAccess != Access.Any)
                {
                    throw new NotSupportedException(
                        "Can't set Write access if Create, Delete, Update access are already setted");
                }
                DeleteAccess = UpdateAccess = CreateAccess = value;
            }
        }

        public string WriteRoles
        {
            get { return UpdateRoles; }
            set
            {
                if (DeleteRoles != null || CreateRoles != null || UpdateRoles != null)
                {
                    throw new NotSupportedException(
                        "Can't set Writes roles if Creates, Deletes, Updates roles are already setted");
                }
                DeleteRoles = UpdateRoles = CreateRoles = value;
            }
        }

        public string[] ReadRolesArray
        {
            get { return ToSplitArray(ReadRoles); }
        }
        public string[] DeleteRolesArray
        {
            get { return ToSplitArray(DeleteRoles); }
        }
        public string[] CreateRolesArray
        {
            get { return ToSplitArray(CreateRoles); }
        }
        public string[] UpdateRolesArray
        {
            get { return ToSplitArray(UpdateRoles); }
        }
        public string[] WriteRolesArray
        {
            get { return ToSplitArray(WriteRoles); }
        }

        protected string[] ToSplitArray(string value)
        {
            return string.IsNullOrEmpty(value) ? new string[0] : value.Split(',').Select(s => s.Trim()).ToArray();
        }
    }
}
